package com.coocaa.x.app.liteplayer.ui;

import java.io.InputStream;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import com.coocaa.x.framework.utils.LogUtils;


public class SkinManager {
    private static String skinName = "";
    private static Context skinContext;
    private static Context appContext;

    public static void setAppContext(Context context) {
        appContext = context.getApplicationContext();
    }


    public static void setSkin(Context context, String skinPkgName) {
        skinName = skinPkgName;
        skinContext = null;
        try {
            appContext = context.getApplicationContext();
            skinContext = context.createPackageContext(skinName, Context.CONTEXT_IGNORE_SECURITY);
//            AssetResManager.getManager().releaseAll();
//            AssetResManager.setSkinAssets(skinContext);
            LogUtils.i("skin", "set Skin successfully, pkg name = " + skinName);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            LogUtils.i("skin", "set Skin failed!!! pkg name = " + skinName);
            skinContext = null;
            e.printStackTrace();
        }
    }

    public static void setBackground(View view, int resId) {
        LogUtils.i("skin", "    setBackground, crt skinName = " + skinName);
        if (skinContext == null) {
            view.setBackgroundResource(resId);
        } else {
            try {
                Drawable drawable = skinContext.getResources().getDrawable(resId);
                if (drawable != null)
                    view.setBackgroundDrawable(drawable);
                else
                    view.setBackgroundResource(resId);
            } catch (Exception e) {
                view.setBackgroundResource(resId);
                LogUtils.i("skin", "finding skin drawable error, msg = " + e.getMessage());
            }
        }
    }

    public static Drawable getResDrawable(int resId) {
        LogUtils.i("skin", "    setBackground, crt skinName = " + skinName);
        if (skinContext == null) {
            return appContext.getResources().getDrawable(resId);
        } else {
            return skinContext.getResources().getDrawable(resId);
        }
    }

    public static void setTextColor(TextView text, int colorId) {
        text.setTextColor(getColor(colorId));
    }

    public static int getColor(int colorId) {
        LogUtils.i("skin", "    setBackground, crt skinName = " + skinName);
        if (skinContext == null) {
            return appContext.getResources().getColor(colorId);
        } else {
            return skinContext.getResources().getColor(colorId);
        }
    }

    public static Bitmap getResourceBitmap(int id) {

        if (skinContext == null) {
            InputStream is = appContext.getResources().openRawResource(id);
            return BitmapFactory.decodeStream(is);
        } else {
            InputStream is = skinContext.getResources().openRawResource(id);
            return BitmapFactory.decodeStream(is);
        }
    }

    public static Bitmap getAssetsBitmap(String path) {
        try {
            if (skinContext == null) {
                Bitmap bm = BitmapFactory.decodeStream(appContext.getAssets().open(path));
                return bm;
            } else {
                Bitmap bm = BitmapFactory.decodeStream(skinContext.getAssets().open(path));
                if (bm != null)
                    return bm;
                else {
                    bm = BitmapFactory.decodeStream(appContext.getAssets().open(path));
                    return bm;
                }
            }
        } catch (Exception e) {
            LogUtils.i("skin", "finding skin assets error, msg = " + e.getMessage());
            return null;
        }
    }

    public static Resources getResources() {
        if (skinContext == null) {
            return appContext.getResources();
        } else {
            return skinContext.getResources();
        }
    }

    public static String getSkinName() {
        return skinName;
    }
}
